package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.enjoyor.soa.traffic.core.efoms.enums.EnumMessage;
import com.enjoyor.soa.traffic.core.efoms.service.ICheckDeviceInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IRepairsInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.ISurveyDeviceInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersInfoService;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

/**
 *
 * 事件监听
 * @ClassNamePushMsg
 * @Description TODO
 * @Author ZSY
 * @Date 2019/1/17 10:07
 * @Version 1.0
 **/
@Service
public class MessagePushListenerServer implements ApplicationListener<MessagePushEventPojo> {

    public static final String OPRATION_NAME = "OPRATION_NAME";

    @Autowired
    private IRepairsInfoService repairsInfoService;

    @Autowired
    private IWorkordersInfoService workordersInfoService;
    
    @Autowired
    private ICheckDeviceInfoService checkDeviceInfoService;
    
    @Autowired
    private ISurveyDeviceInfoService surveyDeviceInfoService;

    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    @Async
    public void onApplicationEvent(MessagePushEventPojo event) {
        String oprationName = event.getOperationName();
        String devTypeCode = event.getDevTypeCode();
        switch (oprationName){
        	case "CHE_MSG_NUMLIST":
        		getCheckPushNumList(devTypeCode);
            	break;
        	case "SUR_MSG_NUMLIST":
        		getSurveyPushNumList(devTypeCode);
            	break;
            case "REP_MSG_APPLY":
                repApplyMessage();
                break;
            case "REP_MSG_PRESS":
                repPressMessage();
                break;
            case "ORD_MSG_SURE":
                WKorderSureMessage();
                break;
            case "ORD_MSG_FACKBACK":
                WKorderFackbackMessage();
                break;
            case "ORD_MSG_PRESS":
                WKorderPresskMessage();
                break;
            case "ORD_MSG_POSTPONE":
                WKorderPostponekMessage();
                break;
            case "ORD_MSG_ISREJECT":
                WKorderRejectkMessage();
                break;
            case "ORD_MSG_OVERTIME":
                getOverDueCount();
                break;
            //======消息提醒========//
            case "RPE_SUBMIT_REMIND":
            	getRepSubmitPushMessage();
                break;
            case "RPE_PRESS_REMIND":
            	getRepPressPushMessage();
                break;
            case "ORDERS_SELL_REMIND":
            	getOrdersSellPushMessage();
                break;
            case "ORDERS_PRESS_REMIND":
            	getOrdersPressPushMessage();
                break;
            case "ORDERS_SCHE_TICK_REMIND":
            	getOrdersScheTickPushMessage();
                break;
            case "ORDERS_DELAY_REMIND":
            	getOrdersDelayPushMessage();
                break;
            case "ORDERS_FINISHED_REMIND":
            	getOrdersFinishedPushMessage();
                break;
            case "ORDERS_DELAY_AUDIT_REMIND":
            	getOrdersDelayAuditPushMessage();
                break;
            case "ORDERS_FINISHED_AUDIT_REMIND":
            	getOrdersFinishedAudit();
                break;
                default:
                    break;
        }

    }

    /**
     * 报修待审核消息提醒推送
     *
     * @return
     */
    public void repApplyMessage() {
        int countrepaite = repairsInfoService.countApplyRepairsInfo(null,null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.REP_MSG_APPLY.getKey(), countrepaite));
    }

    /**
     * 报修催办消息提醒推送
     *
     * @returnR
     */
    public void repPressMessage() {

        String startTime =  TimeHelper.dateToString(new Date(),"yyyy-MM-dd");
         int countRepPress = repairsInfoService.countRepPressInfo(startTime,null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.REP_MSG_PRESS.getKey(), countRepPress));
    }

    /**
     * 维修待确认工单数量查询并推送
     *
     * @return
     */
    public void WKorderSureMessage() {
        int num = workordersInfoService.countSureWKorderInfo(null,null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_SURE.getKey(), num));
    }

    /**
     * 维修反馈工单数量查询并推送
     *
     * @return
     */
    public void WKorderFackbackMessage() {
        int num = workordersInfoService.countFackbackWKorderInfo(
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()),null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_FACKBACK.getKey(), num));
    }

    /**
     * 维修催办工单数量查询并推送
     *
     * @return
     */
    public void WKorderPresskMessage() {
        int num = workordersInfoService.countPressWKorderInfo(
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()),null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_PRESS.getKey(), num));
    }

    /**
     * 维修申请延期工单数量查询并推送
     *
     * @return
     */
    public void WKorderPostponekMessage() {
        int num = workordersInfoService.countPostponeWKorderInfo(
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()),null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_POSTPONE.getKey(), num));
    }

    /**
     * 维修工单已驳回数量推送
     * @return
     */
    public void WKorderRejectkMessage(){
        int num = workordersInfoService.countRejectWKorderInfo(
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()),null);
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_ISREJECT.getKey(), num));
    }

    /**
     * 查询逾期工单并推送
     */
    public void getOverDueCount() {
        int overTime = workordersInfoService.countOverTimeWorkOder();
        System.out.println("逾期工单查询开始：------>>>");
        ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORD_MSG_OVERTIME.getKey(), overTime));
    }
    
    /**
     * 巡检数量推送；
     */
    public void getCheckPushNumList(String devTypeCode){
    	ResultPojo resultPojo = checkDeviceInfoService.getCheckCount(null, null, devTypeCode);
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.CHE_MSG_NUMLIST.getKey(), resultPojo));
    }
    
    /**
     * 监测数量推送；
     */
    public void getSurveyPushNumList(String devTypeCode){
    	ResultPojo resultPojo = surveyDeviceInfoService.getSurveyCount(devTypeCode, null, null);
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.SUR_MSG_NUMLIST.getKey(), resultPojo));
    }
    
    /* 报修提交消息提醒；*/
    public void getRepSubmitPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.RPE_SUBMIT_REMIND.getKey(), "有新报修单提交需审核，注意查看！"));
    }
    
     /*报修催办提醒推送*/
    void getRepPressPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.RPE_PRESS_REMIND.getKey(), "有保修单被催办，注意查看！"));
    }
    
    //工单派发提醒推送
    void getOrdersSellPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_SELL_REMIND.getKey(), "有工单已派发，请注意查收！"));
    }
    
    //工单催办提醒推送
    void getOrdersPressPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_PRESS_REMIND.getKey(), "有工单被催办，请注意查收！"));
    }
    
    //工单进度反馈提醒推送
    void getOrdersScheTickPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_SCHE_TICK_REMIND.getKey(), "有工单进度反馈，请注意查收！"));
    }
    
    //工单延期申请提醒推送
    void getOrdersDelayPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_DELAY_REMIND.getKey(), "有工单延期申请消息，请注意查收！"));
    }
    
    //工单完成反馈
    void getOrdersFinishedPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_FINISHED_REMIND.getKey(), "工单已完成，请注意查看反馈信息！"));
    }
    
    //工单延期审核推送
    void getOrdersDelayAuditPushMessage(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_DELAY_AUDIT_REMIND.getKey(), "逾期工单已被审核，请注意查收"));
    }
    
    //工单完成审核提醒推送
    void getOrdersFinishedAudit(){
    	ServerpushBlock.addMessage(getResultMessage(EnumMessage.ORDERS_FINISHED_AUDIT_REMIND.getKey(), "反馈工单已确认，请注意查收！"));
    }
    
    /**
     * 封装消息体
     * @param oprationName 操作名称
     * @param result 操作结果
     * @return
     */
    private static Map<String,Object> getResultMessage(String oprationName, Object result){
        Map<String, Object> oprationMag = new HashMap<>();
        oprationMag.put(OPRATION_NAME, oprationName);
        oprationMag.put(oprationName, result);
        //Map<String,Object> msg = new HashMap<>();
        return oprationMag;
    }   

}
